/*
 * Dummy web service simulating accessing a database
 */

package db;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.logging.Logger;

/**
 *
 * @author ernestoexposito
 */
@WebService()
public class DBWebService {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "addProduct")
    public String addProduct(@WebParam(name = "id") int id, @WebParam(name = "description") String description, @WebParam(name = "initialPrice") float initialPrice, @WebParam(name = "minPrice") float minPrice, @WebParam(name = "biddingDaysDuration") int biddingDaysDuration) {
        String result = "id:" + id +
                "|description:" + description +
                "|initialPrice:" + initialPrice +
                "|minPrice:" + minPrice +
                "|biddingsDaysDuration:" + biddingDaysDuration;
        BufferedWriter out;
        try {
            out = new BufferedWriter(new FileWriter(new File("../logs/logdb.txt"),true));
            out.write(result);
            out.newLine();
            out.flush();
        } catch (IOException ex) {
            Logger.getLogger(DBWebService.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getProducts")
    public ArrayList<String> getProducts() {
        ArrayList<String> list = new ArrayList<String>();
        try {
            BufferedReader in = new BufferedReader(new FileReader(new File("../logs/logdb.txt")));
            while (in.ready()) {
                StringTokenizer s = new StringTokenizer(in.readLine(), "|");
                list.add(s.nextToken() +"|"+ s.nextToken());
            }

        } catch (IOException ex) {
            Logger.getLogger(DBWebService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;

    }
}
